// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class BulletScript extends cc.Component{

    @property(cc.SpriteFrame)
    explodeEffect: cc.SpriteFrame = null;

    speed: number = 30;

    ducretion: number = 0.016;

    onLoad(){
        this.schedule(this.timerHandler, this.ducretion)
    }

    start () {
        
        
    }

    timerHandler(){
        if(this.node.y >= 500){
            this.unschedule(this.timerHandler)
            // this.node.destroy(); // 销毁节点
            this.beginExplode(); // 添加爆炸效果
            return;
        }

        this.node.y += this.speed;

        this.speed--;
        if(this.speed < 1){
            this.speed = 1
        }
    }

    beginExplode(){
        if(this.explodeEffect === null) return;

        let sp: cc.Sprite = this.node.getComponent(cc.Sprite)
        sp.spriteFrame = this.explodeEffect;

        this.node.setScale(0.001)

        let self = this;
        cc.tween(this.node)
            .to(0.5, {scale: 0.5, opacity: 0})
            .call(function(){
                self.node.destroy();
            })
            .start()
    }
}